package com.binguo.service;

import com.binguo.common.util.JsonUtil;
import com.binguo.entity.CustomerDetailEntity;
import com.binguo.model.mapper.DeviceMsgMapper;
import com.binguo.model.mapper.DeviceMsgTemporaryMapper;
import com.binguo.model.mapper.InitInfoMapper;
import com.binguo.model.model.DeviceMsg;
import com.binguo.model.model.DeviceMsgTemporary;
import com.binguo.model.model.InitInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DeviceMsgService {

    @Resource
    DeviceMsgMapper deviceMsgMapper;
    @Resource
    InitInfoMapper initInfoMapper;
    @Resource
    DeviceMsgTemporaryMapper deviceMsgTemporaryMapper;


    public boolean cmsAddDeviceMsg(Integer shopId, Integer customerId){
        Condition condition = new Condition(DeviceMsgTemporary.class);
        condition.or().andEqualTo("shopId", shopId)
        .andEqualTo("customerId", customerId)
        .andEqualTo("status", 0)
        .andEqualTo("allowStatus", 0)
        ;

        List<DeviceMsgTemporary> deviceMsgTemporaryList =
                deviceMsgTemporaryMapper.selectByCondition(condition);

        if (deviceMsgTemporaryList != null && deviceMsgTemporaryList.size() > 0) {
            DeviceMsgTemporary deviceMsgTemporary = deviceMsgTemporaryList.get(0);

            if(addDeviceMsg(deviceMsgTemporary.getShopId(), deviceMsgTemporary.getCustomerId(),
                    deviceMsgTemporary.getCustomerName(), deviceMsgTemporary.getFaceUrl(),
                    "1", deviceMsgTemporary.getFaceId())){
                DeviceMsgTemporary temporary = new DeviceMsgTemporary();
                temporary.setId(deviceMsgTemporary.getId());
                temporary.setStatus(1);
                deviceMsgTemporaryMapper.updateByPrimaryKeySelective(temporary);
                return true;
            }
        }
        return false;
    }
    /**
     *
     * @param shopId
     * @param customerId
     * @param customerName
     * @param faceUrl
     * @param type 1 add 0 del
     * @return
     */
    public Boolean addDeviceMsg(Integer shopId, Integer customerId,
                               String customerName, String faceUrl, String type
                            ,String faceId
    ){
        Condition condition = new Condition(InitInfo.class);
        condition.or().andEqualTo("shopid", shopId);

        List<InitInfo> initInfoList = initInfoMapper.selectByCondition(condition);
        if (initInfoList != null && initInfoList.size() > 0) {
            DeviceMsg deviceMsg = new DeviceMsg();
            deviceMsg.setCreateTime(new Date());
            deviceMsg.setMsgType(1);
            deviceMsg.setReadStaus(0);
            deviceMsg.setShopId(shopId);

            CustomerDetailEntity customerDetailForm = new CustomerDetailEntity();
            customerDetailForm.setId(customerId.toString());
            customerDetailForm.setName(customerName);
            customerDetailForm.setFaceUrl(faceUrl);
            customerDetailForm.setType(type);
            customerDetailForm.setFaceId(faceId);

            try {
                deviceMsg.setMsgContent(JsonUtil.toJson(customerDetailForm));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return false;
            }


            initInfoList.stream().forEach(initInfo -> {
                deviceMsg.setDeviceId(initInfo.getDeviceid());
                deviceMsg.setId(null);
                deviceMsgMapper.insertSelective(deviceMsg);
            });
        }

        return true;
    }
}
